import { Injectable } from '@nestjs/common';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Role } from './entities/role.entity';
import { Repository } from 'typeorm';

@Injectable()
export class RolesService {
  constructor(
    @InjectRepository(Role) private roleRepository: Repository<Role>,
  ) {}
  async create(createRoleDto: CreateRoleDto) {
    const roleInfo = createRoleDto;
    const role = await this.roleRepository.findOne({
      where: {
        roleNo: roleInfo.roleNo,
      },
    });
    if (role) {
      return {
        code: 1,
        message: '该角色编码已存在！',
      };
    } else {
      await this.roleRepository.save(roleInfo);
      return {
        code: 0,
        message: '新建成功',
      };
    }
  }

  findAll() {
    return this.roleRepository.find();
  }

  findOne(id: string) {
    return this.roleRepository.findOneBy({ id });
  }

  update(id: string, updateRoleDto: UpdateRoleDto) {
    return this.roleRepository.update(id, updateRoleDto);
  }

  remove(id: string) {
    return this.roleRepository.delete(id);
  }
}
